import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileReader;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.util.List;
import java.util.Vector;
import java.util.regex.Pattern;
import edu.columbia.ccls.GISDatabase;
import edu.columbia.ccls.JDBCDatabase;

/**
 * 
 */

/**
 * @author Ashish Tomar
 *
 */
public class GenerateCableKMLInfo
{
	class Record 
	{
		String label;
		String color;
		double toLon;
		double toLat;
		double fromLon;
		double fromLat;

		Record(String label, String color, double lon, double lat,double lon2, double lat2) 
		{
			this.label = label;
			this.color = color;
			//this.geoLat = geoLat;
			this.toLon = lon;
			this.toLat = lat;
			this.fromLat=lat2;
			this.fromLon=lon2;
		}
	}

	Placemark placemark;
	enum GISType {POLYGON, LINESTRING};
	String kmlString = "";

	//Initialize the folder name to manhole events MHE for now 
	String tType = "MHF";
	Vector<Record> data = new Vector<Record> ();

	String styleURL;

	String preamble = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
	"<kml xmlns=\"http://www.opengis.net/kml/2.2\">" +
	"<Document>" +
	"<name>%s</name>\n";

	String style="     		<description>Cables:Colored according to the number of cables between two structures.</description> " +
	"			 <Style id=\"basic1\">       			<LineStyle>         			<color>af407E00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic2\">       			<LineStyle>         			<color>af417D00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic3\">       			<LineStyle>         			<color>af417C00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic4\">       			<LineStyle>         			<color>af427B00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic5\">       			<LineStyle>         			<color>af427A00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic6\">       			<LineStyle>         			<color>af437900</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic7\">       			<LineStyle>         			<color>af437800</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic8\">       			<LineStyle>         			<color>af447700</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic9\">       			<LineStyle>         			<color>af447600</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic10\">       			<LineStyle>         			<color>af457500</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic11\">       			<LineStyle>         			<color>af457400</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic12\">       			<LineStyle>         			<color>af467300</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic13\">       			<LineStyle>         			<color>af467200</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic14\">       			<LineStyle>         			<color>af477100</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic15\">       			<LineStyle>         			<color>af477000</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic16\">       			<LineStyle>         			<color>af486F00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic17\">       			<LineStyle>         			<color>af486E00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic18\">       			<LineStyle>         			<color>af496D00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic19\">       			<LineStyle>         			<color>af496C00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic20\">       			<LineStyle>         			<color>af4A6B00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic21\">       			<LineStyle>         			<color>af4A6A00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic22\">       			<LineStyle>         			<color>af4B6900</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic23\">       			<LineStyle>         			<color>af4B6800</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic24\">       			<LineStyle>         			<color>af4C6700</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic25\">       			<LineStyle>         			<color>af4C6600</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic26\">       			<LineStyle>         			<color>af4D6500</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic27\">       			<LineStyle>         			<color>af4D6400</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic28\">       			<LineStyle>         			<color>af4E6300</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic29\">       			<LineStyle>         			<color>af4E6200</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic30\">       			<LineStyle>         			<color>af4F6100</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic31\">       			<LineStyle>         			<color>af4F6000</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic32\">       			<LineStyle>         			<color>af505F00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic33\">       			<LineStyle>         			<color>af505E00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic34\">       			<LineStyle>         			<color>af515D00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic35\">       			<LineStyle>         			<color>af515C00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic36\">       			<LineStyle>         			<color>af525B00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic37\">       			<LineStyle>         			<color>af525A00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic38\">       			<LineStyle>         			<color>af535900</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic39\">       			<LineStyle>         			<color>af535800</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic40\">       			<LineStyle>         			<color>af545700</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic41\">       			<LineStyle>         			<color>af545600</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic42\">       			<LineStyle>         			<color>af555500</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic43\">       			<LineStyle>         			<color>af555400</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic44\">       			<LineStyle>         			<color>af565300</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic45\">       			<LineStyle>         			<color>af565200</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic46\">       			<LineStyle>         			<color>af575100</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic47\">       			<LineStyle>         			<color>af575000</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic48\">       			<LineStyle>         			<color>af584F00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic49\">       			<LineStyle>         			<color>af584E00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic50\">       			<LineStyle>         			<color>af594D00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic51\">       			<LineStyle>         			<color>af594C00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic52\">       			<LineStyle>         			<color>af5A4B00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic53\">       			<LineStyle>         			<color>af5A4A00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic54\">       			<LineStyle>         			<color>af5B4900</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic55\">       			<LineStyle>         			<color>af5B4800</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic56\">       			<LineStyle>         			<color>af5C4700</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic57\">       			<LineStyle>         			<color>af5C4600</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic58\">       			<LineStyle>         			<color>af5D4500</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic59\">       			<LineStyle>         			<color>af5D4400</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic60\">       			<LineStyle>         			<color>af5E4300</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic61\">       			<LineStyle>         			<color>af5E4200</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic62\">       			<LineStyle>         			<color>af5F4100</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic63\">       			<LineStyle>         			<color>af5F4000</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic64\">       			<LineStyle>         			<color>af603F00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic65\">       			<LineStyle>         			<color>af603E00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic66\">       			<LineStyle>         			<color>af613D00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic67\">       			<LineStyle>         			<color>af613C00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic68\">       			<LineStyle>         			<color>af623B00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic69\">       			<LineStyle>         			<color>af623A00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic70\">       			<LineStyle>         			<color>af633900</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic71\">       			<LineStyle>         			<color>af633800</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic72\">       			<LineStyle>         			<color>af643700</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic73\">       			<LineStyle>         			<color>af643600</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic74\">       			<LineStyle>         			<color>af653500</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic75\">       			<LineStyle>         			<color>af653400</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic76\">       			<LineStyle>         			<color>af663300</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic77\">       			<LineStyle>         			<color>af663200</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic78\">       			<LineStyle>         			<color>af673100</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic79\">       			<LineStyle>         			<color>af673000</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic80\">       			<LineStyle>         			<color>af682F00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic81\">       			<LineStyle>         			<color>af682E00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic82\">       			<LineStyle>         			<color>af692D00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic83\">       			<LineStyle>         			<color>af692C00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic84\">       			<LineStyle>         			<color>af6A2B00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic85\">       			<LineStyle>         			<color>af6A2A00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic86\">       			<LineStyle>         			<color>af6B2900</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic87\">       			<LineStyle>         			<color>af6B2800</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic88\">       			<LineStyle>         			<color>af6C2700</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic89\">       			<LineStyle>         			<color>af6C2600</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic90\">       			<LineStyle>         			<color>af6D2500</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic91\">       			<LineStyle>         			<color>af6D2400</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic92\">       			<LineStyle>         			<color>af6E2300</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic93\">       			<LineStyle>         			<color>af6E2200</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic94\">       			<LineStyle>         			<color>af6F2100</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic95\">       			<LineStyle>         			<color>af6F2000</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic96\">       			<LineStyle>         			<color>af701F00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic97\">       			<LineStyle>         			<color>af701E00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic98\">       			<LineStyle>         			<color>af711D00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic99\">       			<LineStyle>         			<color>af711C00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic100\">       			<LineStyle>         			<color>af721B00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic101\">       			<LineStyle>         			<color>af721A00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic102\">       			<LineStyle>         			<color>af731900</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic103\">       			<LineStyle>         			<color>af731800</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic104\">       			<LineStyle>         			<color>af741700</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic105\">       			<LineStyle>         			<color>af741600</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic106\">       			<LineStyle>         			<color>af751500</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic107\">       			<LineStyle>         			<color>af751400</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic108\">       			<LineStyle>         			<color>af761300</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic109\">       			<LineStyle>         			<color>af761200</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic110\">       			<LineStyle>         			<color>af771100</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic111\">       			<LineStyle>         			<color>af771000</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic112\">       			<LineStyle>         			<color>af780F00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic113\">       			<LineStyle>         			<color>af780E00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic114\">       			<LineStyle>         			<color>af790D00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic115\">       			<LineStyle>         			<color>af790C00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic116\">       			<LineStyle>         			<color>af7A0B00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic117\">       			<LineStyle>         			<color>af7A0A00</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic118\">       			<LineStyle>         			<color>af7B0900</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic119\">       			<LineStyle>         			<color>af7B0800</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic120\">       			<LineStyle>         			<color>af7C0700</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic121\">       			<LineStyle>         			<color>af7C0600</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic122\">       			<LineStyle>         			<color>af7D0500</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic123\">       			<LineStyle>         			<color>af7D0400</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic124\">       			<LineStyle>         			<color>af7E0300</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic125\">       			<LineStyle>         			<color>af7E0200</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic126\">       			<LineStyle>         			<color>af7F0100</color>         			<width>4</width>       			</LineStyle>       		</Style> 			 <Style id=\"basic127\">       			<LineStyle>         			<color>af800000</color>         			<width>4</width>       			</LineStyle>       		</Style>";

	String post = "</Document>" +
	"</kml>";

	String pointFormat = "<Placemark><name>%s</name><styleUrl>#%s</styleUrl>" +
	"<LineString>					<extrude>0</extrude> 	<tessellate>1</tessellate> " + 
	"<altitudeMode>clampToGround </altitudeMode>					<coordinates>%f,%f,%d %f,%f,%d					</coordinates>				</LineString>		</Placemark>\n";

	Pattern p1 = Pattern.compile("<coordinates>");
	Pattern p2 = Pattern.compile("</coordinates>");

	//This is the constructor class
	public GenerateCableKMLInfo(String inputString, String trbType, Placemark placemark, List<String> getBorough, int case_cable) throws Exception
	{
		this.placemark = placemark;
		this.styleURL = 	"<Style id=\"%1$s%2$s\">" +
		"<IconStyle>" +
		"<scale>1.</scale>"+
		"<Icon>" +
		"<href>%1$s%2$s.png</href>"+	
		"</Icon>" +
		"<hotSpot x=\"16\" y=\"16\" xunits=\"pixels\" yunits=\"pixels\"/>"+
		"</IconStyle>" +
		"</Style>\n";
		tType=trbType;
		String query;
		//Adjust the query based on the trouble type
		query = "select distinct * from " ;
		if (tType.equalsIgnoreCase("Cable")) {
			query += "("+ getCableQuery(getBorough, inputString,case_cable) + ") as s";

		}
		else {

		}


		//System.out.println("The query finally is"+ queryPoly);
		System.out.println("No of boroughs selected are -- from GenKML: "+getBorough.size());

		getKML(query, tType);

	}
	//Use the main method to test the kml file generated
	public static void main(String [] args) throws Exception
	{
		System.out.println("Test Main");
		String fileName = args[0];
		System.out.println("input file name is " + fileName);
		String poly = "";
		String tt="MHF";
		BufferedReader reader = new BufferedReader(new FileReader(fileName));//  ( new InputStreamReader(System.in));
		String line = "";
		while ((line = reader.readLine()) != null) 
		{	
			poly += line;
			//System.out.println("The line is "+poly);
		}
		//System.out.println(new GenerateKMLInfo(poly,tt, new Placemark()).kmlString + "\tGenerateKML.java @ 183", getBorough); //Debug
	}

	//Method that generates the kml file
	void getKML(String queryPoly, String title) throws Exception {
		try
		{
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			PrintStream stream = new PrintStream(baos);

			//Getting the database connections
			JDBCDatabase db = new GISDatabase();
			db.setUser("calm_user");
			db.setDatabaseName("coned");
			//db.setPort(5432);

			//KML Preamble - the beginning of the KML
			stream.printf(preamble, title);

			stream.println(style);

			//			stream.printf(preFolder, title);
			data = new Vector<Record> ();
			System.out.println("Query is: "+queryPoly);
			ResultSet rs = db.executeQuery(queryPoly);
			while (rs!=null && rs.next()) 
			{
				String ticket = rs.getString("label");
				String act = rs.getString("color");
				double x = rs.getDouble("lon");
				double y = rs.getDouble("lat");
				double xx = rs.getDouble("lon2");
				double yy = rs.getDouble("lat2");

				data.add(new Record(ticket, act, x, y, xx,yy));
			}
			System.out.println("Data size: "+data.size());
			String icon=this.placemark.getName();
			System.out.println("The icon to be used is " + icon);

			for (Record row: data) 
			{
				String name = row.label;
				String act = row.color;
				int id=(int)(Double.parseDouble(act)*128);
				int alt=2;
				stream.printf(pointFormat, name, "basic"+id,row.toLon,row.toLat,alt,row.fromLon, row.fromLat,alt); 
			}
			//			stream.println(postFolder);
			stream.println(post);
			kmlString = baos.toString();
			//db.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}

	}

	public String getCableQuery(List<String> getBorough, String inputString, int caseCable) throws Exception {


		int boroughType=getBoroughType(getBorough);

		String rankQuery;

		if(caseCable==0){
			switch(boroughType){

			case 1:// Only Manhattan
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				System.out.println("Manhattan selected for Cable ");
				break;

			case 2:// Only Brooklyn
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);

				System.out.println("Brooklyn selected for Cable ");
				break;


			case 3: //Only Bronx

				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);

				System.out.println("Bronx selected for Cable ");

				break;

			case 4: //Only Queens

				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);

				System.out.println("queens selected for Cable ");

				break;

			case 5://Only Brooklyn and manhattan

				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				System.out.println("Brooklyn and manhattan boroughs selected for Cable ");

				break;

			case 6://Only Bronx and manhattan

				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				System.out.println("Bronx and manhattan boroughs selected for Cable ");

				break;

			case 7: //Only Bronx and brooklyn

				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				System.out.println("Bronx and brooklyn boroughs selected for Cable ");

				break;


			case 8://Only queens and manhattan

				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				System.out.println("queens and manhattan boroughs selected for Cable ");

				break;

			case 9: //Only queens and brooklyn

				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				System.out.println("queens and brooklyn boroughs selected for Cable ");

				break;

			case 10: //Only Bronx and queens

				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				System.out.println("Bronx and queens boroughs selected for Cable ");

				break;

			case 11://ME, BE, XE
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				System.out.println("ME, BE, XE boroughs selected for Cable ");
				break;


			case 12://ME, BE, QE
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				System.out.println("ME, BE, QE boroughs selected for Cable ");
				break;

			case 13://ME, QE, XE
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				System.out.println("ME, QE, XE boroughs selected for Cable ");
				break;


			case 14://QE, BE, XE
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				System.out.println("QE, BE, XE boroughs selected for Cable ");
				break;


			default://all
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,1);
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  where " ;
				rankQuery += queryPolygon(inputString, getBorough,2);
				System.out.println("all of the boroughs selected for Cable ");
				break;



			}




		}else{

			switch(boroughType){

			case 1:// Only Manhattan
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  " ;

				System.out.println("Manhattan selected for only Cable ");
				break;

			case 2:// Only Brooklyn
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn   " ;

				System.out.println("Brooklyn selected for only Cable ");
				break;

			case 3: //Only Bronx

				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx   " ;

				System.out.println("Bronx selected for only Cable ");

				break;

			case 4: //Only Queens

				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens   " ;

				System.out.println("Queens selected for only Cable ");

				break;

			case 5://Only Brooklyn and manhattan
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn   " ;

				System.out.println("Brooklyn and manhattan boroughs selected for only Cable ");
				break;

			case 6://Only Bronx and manhattan
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx   " ;

				System.out.println(" Bronx and manhattan boroughs selected for only Cable ");
				break;

			case 7: //Only Bronx and brooklyn
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn   " ;

				System.out.println(" Bronx and brooklyn boroughs selected for only Cable ");
				break;

			case 8://Only queens and manhattan
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens    " ;

				System.out.println(" queens and manhattan boroughs selected for only Cable ");
				break;


			case 9: //Only queens and brooklyn
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens   " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn   " ;

				System.out.println(" queens and brooklyn boroughs selected for only Cable ");
				break;


			case 10: //Only Bronx and queens
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx  " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens    " ;

				System.out.println(" Bronx and queens boroughs selected for only Cable ");
				break;


			case 11://ME, BE, XE
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn   " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx    " ;

				System.out.println("ME, BE, XE of the boroughs selected for only Cable ");
				break;

			case 12://ME, BE, QE
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn   " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens      " ;

				System.out.println("ME, BE, QE of the boroughs selected for only Cable ");
				break;

			case 13://ME, QE, XE
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens     " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx    " ;

				System.out.println("ME, QE, XE of the boroughs selected for only Cable ");
				break;

			case 14://QE, BE, XE
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens    " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn   " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx    " ;

				System.out.println("QE, BE, XE of the boroughs selected for only Cable ");
				break;


			default://all
				rankQuery = "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density  " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_brooklyn   " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_bronx    " ;
				rankQuery +=" union ";
				rankQuery += "select  label,color,lon1 as lon,lat1 as lat,lon2,lat2  from axinia.nb_cable_density_queens    " ;

				System.out.println("all of the boroughs selected for only Cable ");
				break;

			}

		}
		return rankQuery;
	}
	/*
	private int getBoroughType(List<String> getBorough) {

		int boroughType=0;
		if(getBorough.size()>2||getBorough.size()==0){
			//Consider all of the boroughs
			boroughType=0;
		}
		else if(getBorough.size()==1){
			// only one borough
			String borough=getBorough.get(0);
			if(borough.equalsIgnoreCase("ME")){
				boroughType=1;
			}
			else if(borough.equalsIgnoreCase("BE")){
				boroughType=2;
			}
			else if(borough.equalsIgnoreCase("XE")){
				boroughType=3;
			}
			else{
				boroughType=0;
			}
			}
		else if(getBorough.size()==2){
			// only one borough

			if(getBorough.contains("ME")&& getBorough.contains("BE")){
				boroughType=4;
			}
			else if(getBorough.contains("ME")&& getBorough.contains("XE")){
				boroughType=5;
			}
			else if(getBorough.contains("XE")&& getBorough.contains("BE")){
				boroughType=6;
			}
			else{
				boroughType=0;
			}
		}


		return boroughType;
	}
	 */
	public String queryPolygon(String inputString, List<String> getBorough, int cableType) throws Exception {
		String query = new String();

		String kmlcontent =inputString;
		ByteArrayInputStream inputStream = new ByteArrayInputStream(inputString.getBytes());
		int mark = 0;
		int c;
		while ((c = inputStream.read()) != -1){
			if (c == '<') {
				break;
			}
			mark++;
			inputStream.mark(mark);
		}
		inputStream.reset();
		if (c == -1 ) {
			System.out.println("Crashing!!!");
			return null; // we're returning null! isn't that a recipe for disaster?
		}
		//System.out.println("Before XMLReader--");
		XMLReader reader = new XMLReader(inputStream, kmlcontent);
		GISType type;
		String googleCoordinates = reader.getString("//edx:Polygon/edx:outerBoundaryIs/edx:LinearRing/edx:coordinates"); 
		if (!googleCoordinates.equals("")) 
		{
			query += " contains(polygonfromtext('POLYGON((";
			type = GISType.POLYGON;
		}
		else {
			googleCoordinates = reader.getString("//edx:LineString/edx:coordinates");

			if (! googleCoordinates.equals("")) {
				query += " distance(linestringfromtext('LINESTRING(";
				type = GISType.LINESTRING;
			}
			else {
				System.out.println("Throwing Exception Coordinates---");
				throw new Exception(inputString);
			}
		}
		System.out.println("The google coordinates  are --- "+ googleCoordinates);

		String [] points = googleCoordinates.split(" ");
		for (int p = 0 ; p < points.length; p++) {
			String [] coords = points[p].split(",");
			query += coords[0] + " " + coords[1];
			if (p < points.length -1) {
				query += ", ";
			}
		}

		if (type == GISType.POLYGON) {
			if(cableType==1){
				query += "))', 4326), pointfromtext('POINT(' || lon1 || '  ' || lat1 || ')',  4326))" ;
			}
			else if(cableType==2){
				query += "))', 4326), pointfromtext('POINT(' || lon2 || '  ' || lat2 || ')',  4326))" ;
			}
		}
		else if (type == GISType.LINESTRING) {
			query += ")', 4326), pointfromtext('POINT(' || lon || '  ' || lat || ')',  4326)) < .0002";
		}
		return query;
	}


	/**
	 * use it when you add queens
	 * @param getBorough
	 * @return
	 */
	private int getBoroughType(List<String> getBorough) {

		int boroughType=0;

		if(getBorough.size()>3||getBorough.size()==0){
			//Consider all of the boroughs
			boroughType=0;
		}
		else if(getBorough.size()==1){
			// only one borough
			String borough=getBorough.get(0);
			if(borough.equalsIgnoreCase("ME")){
				boroughType=1;
			}
			else if(borough.equalsIgnoreCase("BE")){
				boroughType=2;
			}
			else if(borough.equalsIgnoreCase("XE")){
				boroughType=3;
			}
			else if(borough.equalsIgnoreCase("QE")){
				boroughType=4;
			}
			else{
				boroughType=0;
			}
		}
		else if(getBorough.size()==2){
			// only one borough

			if(getBorough.contains("ME")&& getBorough.contains("BE")){
				boroughType=5;
			}
			else if(getBorough.contains("ME")&& getBorough.contains("XE")){
				boroughType=6;
			}
			else if(getBorough.contains("XE")&& getBorough.contains("BE")){
				boroughType=7;
			}
			else if(getBorough.contains("ME")&& getBorough.contains("QE")){
				boroughType=8;
			}
			else if(getBorough.contains("QE")&& getBorough.contains("BE")){
				boroughType=9;
			}
			else if(getBorough.contains("XE")&& getBorough.contains("QE")){
				boroughType=10;
			}
			else{
				boroughType=0;
			}
		}
		else if(getBorough.size()==3){
			if(getBorough.contains("ME")&& getBorough.contains("BE") &&  getBorough.contains("XE")){
				boroughType=11;
			}
			else if(getBorough.contains("ME")&& getBorough.contains("QE") && getBorough.contains("BE")){
				boroughType=12;
			}
			else if(getBorough.contains("ME")&& getBorough.contains("QE") && getBorough.contains("XE")){
				boroughType=13;
			}
			else if(getBorough.contains("XE")&& getBorough.contains("QE") && getBorough.contains("BE")){
				boroughType=14;
			}
			else{
				boroughType=0;
			}
		}


		return boroughType;
	}




}
